package com.ruoyi.project.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.experiment.domain.Circulation;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.finance.model.Ids;
import com.ruoyi.project.model.BriefProject;
import com.ruoyi.project.model.ExperimentDetails;
import com.ruoyi.project.model.PExperiment;
import com.ruoyi.project.service.IHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/handle")
public class HandleController extends BaseController {

    @Autowired
    IHandleService service;


    @GetMapping("/list")
    public TableDataInfo listBriefProject(BriefProject project){
        List<BriefProject> briefProjects = service.listBriefProject(project);
        return getDataTable(briefProjects);
    }

    @GetMapping("/experimentDetails")
    public TableDataInfo listExperimentDetails(ExperimentDetails experimentDetails){
        startPage();
        List<Experiment> res = service.listExperimentDetails(experimentDetails);
        return getDataTable(res);
    }

    @GetMapping("/list/post")
    public TableDataInfo listPost(PExperiment post){
        startPage();
        List<PExperiment> pPosts=null;
        if (post.getHasConfirm()){
            pPosts=service.listPostByProjectId2(post);
        }else {
            pPosts= service.listPostByProjectId(post);
        }
        return getDataTable(pPosts);
    }
    @GetMapping("/post/confirm")
    public AjaxResult confirmPost(Long id){
        service.confirmPost(id);
        return success();
    }
    @PostMapping("/post/confirms")
    public AjaxResult confirmPosts(@RequestBody Ids ids){
        for(Long id : ids.getIds()){
            service.confirmPost(id);
        }
        return success();
    }

    /**
     * 修改实验信息
     * @param ori
     * @return
     */
    @PostMapping("/experiment/update")
    public AjaxResult updatePost(@RequestBody Experiment ori){
        Experiment exp = new Experiment();
        exp.setExperimentId(ori.getExperimentId());
        exp.setJobPrice(ori.getJobPrice());
        exp.setExperimentPrice(ori.getExperimentPrice());
        exp.setExperimentNumber(ori.getExperimentNumber());
        service.updateExperiment(exp);
        return success();
    }

//    @GetMapping("/post/back")
//    public AjaxResult backPost(Long id){
//        service.backPost(id);
//        return success();
//    }
//    @PostMapping("/post/backs")
//    public AjaxResult backPost(@RequestBody Ids ids){
//        for(Long id : ids.getIds()){
//            service.backPost(id);
//        }
//        return success();
//    }
    @GetMapping("/list/experiment")
    public TableDataInfo listExperiment(PExperiment post){
        startPage();
        List<PExperiment> pPosts=null;
        if (post.getHasConfirm()){
            pPosts=service.listExperimentByProjectId2(post);
        }else {
            pPosts= service.listExperimentByProjectId(post);
        }
        return getDataTable(pPosts);
    }
    @GetMapping("/experiment/confirm")
    public AjaxResult confirmExperiment(Long id){
        service.confirmExperiment(id);
        return success();
    }
    @PostMapping("/experiment/confirms")
    public AjaxResult confirmExperiments(@RequestBody Ids ids){
        for(Long id : ids.getIds()){
            service.confirmExperiment(id);
        }
        return success();
    }
    @GetMapping("/experiment/back")
    public AjaxResult backExperiment(Long id){
        service.backExperiment(id);
        return success();
    }
    @PostMapping ("/experiment/backs")
    public AjaxResult backExperiments(@RequestBody Ids ids){
        for(Long id : ids.getIds()){
            service.backExperiment(id);
        }
        return success();
    }
    @GetMapping("/finish")
    public AjaxResult finish(Long id){
        String finish = service.finish(id);
        if (finish==null)
            return success("");
        else
            return success(finish);
    }

    /**
     * 申请及归还样品
     * @param circulation
     * @return
     */
    @PostMapping("flume")
    public AjaxResult flume(@RequestBody Circulation circulation){
        service.flume(circulation);
        return success();
    }
}
